<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>多条件对应名称(使用map优化)</title>
	</head>

	<body>
		<script>
			/**
			 * @desc 现在把问题升级一下， 以前按钮点击时候只需要判断status，现在还需要判断用户的身份：
			 * @param status{状态}
			 * @param identity{身份}
			 */

			//if else 版本
			const onButtonClick = (status, identity) => {
				if (identity == 'guest') {
					if (status == 1) {
						//do sth
					} else if (status == 2) {
						//do sth
					} else if (status == 3) {
						//do sth
					} else if (status == 4) {
						//do sth
					} else if (status == 5) {
						//do sth
					} else {
						//do sth
					}
				} else if (identity == 'master') {
					if (status == 1) {
						//do sth
					} else if (status == 2) {
						//do sth
					} else if (status == 3) {
						//do sth
					} else if (status == 4) {
						//do sth
					} else if (status == 5) {
						//do sth
					} else {
						//do sth
					}
				}
			}
			// 使用 map 数据结构优化
			const actions = new Map([
				[
					'guest_1',
					() => {
						/*do sth*/
					},
				],
				[
					'guest_2',
					() => {
						/*do sth*/
					},
				],
				[
					'guest_3',
					() => {
						/*do sth*/
					},
				],
				[
					'guest_4',
					() => {
						/*do sth*/
					},
				],
				[
					'guest_5',
					() => {
						/*do sth*/
					},
				],
				[
					'master_1',
					() => {
						/*do sth*/
					},
				],
				[
					'master_2',
					() => {
						/*do sth*/
					},
				],
				[
					'master_3',
					() => {
						/*do sth*/
					},
				],
				[
					'master_4',
					() => {
						/*do sth*/
					},
				],
				[
					'master_5',
					() => {
						/*do sth*/
					},
				],
				[
					'default',
					() => {
						/*do sth*/
					},
				],
			])
			const onButtonClick = (identity, status) => {
				let action = actions.get(`${identity}_${status}`) || actions.get('default')
				action()
			}
			onButtonClick('master', 1)
		</script>
	</body>
</html>
